<template>
  <div class="modal-con">
    <div class="clarity"></div>
    <div class="modal">
      <div class="modal-title">{{dataInfo.title}}</div>
      <div class="modal-body">
        <div class="delete-text" v-if="dataInfo.title=='删除'">你确定要删除?</div>
        <div v-if="dataInfo.title!=='删除'">
          <div class="type-con">
            <span class="type">sync_type:</span>
            <select class="select-type" @change="selectType()" v-model="dataInfo.type">
              <option v-for="select in lists" :value="select">{{select}}</option>
            </select>
          </div>
          <div class="source-con">
            <span class="source-input">sync_source:</span>
            <input type="text" class="source" v-model="dataInfo.source" />
          </div>
          <div class="activated-con">
            <span class="activated">activated:</span>
            <select class="select-activated" @change="selectNumber()" v-model="dataInfo.activated">
              <option v-for="number in numbers" :value="number">{{number}}</option>
            </select>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button class="confirm-btn" @click="confirm(dataInfo)">确定</button>
        <button class="cancel-btn" @click="cancel(dataInfo)">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
  import baseUrl from '../../static/address.js'
  export default {
    name: 'modal',
    data() {
      return {
        lists: ['DDL','DML','CSV'],
        numbers: ['0','1']
      }
    },
    props: ['dataInfo','items'],
    methods: {
      selectType() {
        console.log('type',this.dataInfo.type)
      },
      selectNumber() {
        console.log('number',this.dataInfo.activated)
      },
      //点击确定
      confirm(option) {
        if(option.title=="添加") {
          console.log('option',option);
          //添加请求
          this.$axios({
            method: 'post',
            url: baseUrl.host + '/admin/' + option.client_code + '/actions',
            headers: {"x-access-token": option.token},
            data: {
              sync_type: option.type,
              sync_source: option.source,
              activated: parseInt(option.activated)
            } 
          }).then((res)=>{
            console.log('添加',res);
            //获取一次数据，更新变化
            this.getItem(option);
          }).catch((err)=>{
            console.log('错误',err)
          })
        }else if(option.title=="更新") {
          //修改请求
          this.$axios({
            method: 'patch',
            url: baseUrl.host + '/admin/' + option.client_code + '/actions/' + option.id,
            headers: {"x-access-token": option.token},
            data: {
              sync_type: option.type,
              sync_source: option.source,
              activated: parseInt(option.activated)
            }
          }).then((res)=>{
            console.log('修改',res)
            //获取一次数据，更新变化
            this.getItem(option);
          }).catch((err)=>{
            console.log(err)
          })
        }else {
          console.log('option',option);
          //删除请求
          this.$axios.delete(baseUrl.host + '/admin/' +option.client_code + '/actions/' + option.action_id,
          {
            headers: {
              "x-access-token": option.token
            }
          }).then((res=>{
            console.log('删除',res)
            //获取一次数据，更新变化
            this.getItem(option);
          })).catch((err)=>{
            console.log(err)
          })
        }

      },
      //取消操作
      cancel(option) {
        this.getItem(this.dataInfo)
      },
      //获取列表数据的请求
      getItem(dataInfo) {
        console.log('Info',dataInfo)
        this.$axios.get(baseUrl.host + '/admin/'+dataInfo.client_code + '/actions',{
          headers: {
            "x-access-token": dataInfo.token
          }
        }).then((res)=>{
          var items = res.data.items;
          console.log('变化后',items)
          let dataInfo = {ok: false};
          this.$emit('ievent',dataInfo,items);
        })
      }
    }
  }
</script>

<style scoped>
  .clarity {
    height:100%;
    width:100%;
    background-color: #aaa;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    opacity: 0.5;
  }
  .modal {
    position:fixed;
    z-index: 20;
    width: 500px;
    height: 320px;
    background-color: #fff;
    top: 50%;
    left:50%;
    transform: translate(-50%, -50%);
    /*border: 1px solid;
    border-radius: 5px*/
  }
  .modal-title{
    height: 40px;
    line-height: 40px;
    font-size: 25px;
    text-align: center;
    border-bottom: 1px solid;
    background-color: #185089;
    color:#FFFFFF;
  }
  .modal-body {
    text-align: left;
    margin: 30px 0;
  }
  .source-input,.type,.activated {
    display: inline-block;
    width: 120px;
    text-align: right;
    font-size:20px;
  }
  .source {
    width: 360px;
    height: 30px;
  }
  .select-type,.select-activated {
    display: inline-block;
    width: 120px;
    height: 30px;
    font-size: 20px;
  }
  .type-con,.source-con,.activated-con {
    margin:15px 0;
  }
  .delete-text {
    font-size: 20px;
    text-align: center;
    height: 100px;
    line-height: 100px;
  }
  .modal-footer {
    text-align: center;
    padding: 10px 0;
  }
  .confirm-btn,.cancel-btn {
    font-size: 18px;
    padding:3px 8px;
    border: 1px solid #41708A;
    border-radius: 5px;
    background-color: #87CEFA;
    cursor: pointer;
  }
  .cancel-btn {
    margin-left: 10px;
  }

</style>